<template>
  <!-- 日常考核 -->
  <div class="root" element-loading-text="加载中">
    <div v-if="!showDailyRecord">
      <div v-if="flag !=='detail'" class="btn-group">
        <el-button type="primary" size="mini" @click="handleEdit">新增评估</el-button>
        <el-button type="primary" size="mini" @click="handleCheckDetail">查看历史评估记录</el-button>
      </div>
      <el-table
        v-loading="tableLoading"
        element-loading-text="加载中"
        :data="tableData"
        :span-method="objectSpanMethod"
        border
        highlight-current-row
        :header-cell-style="{background:'#F6F6F6', borderRight:'1px solid #EBEEF5'}"
        style="width: 100%; margin-top: 20px"
      >
        <el-table-column prop="scoreType" label="考核类别" min-width="100" />
        <el-table-column prop="totalScore" label="考核总分" min-width="150" />
        <el-table-column prop="scoreDesc" label="考核说明" show-overflow-tooltip min-width="150" />
        <el-table-column prop="validStartTime" label="评分有效开始时间" min-width="150" />
        <el-table-column prop="validEndTime" label="评分有效截止日期" min-width="150" />
        <el-table-column prop="detailItem" label="考核明细项目" min-width="200" />
        <el-table-column prop="grading" label="评分标准" min-width="150" />
        <el-table-column prop="itemScoreDesc" label="评分说明" show-overflow-tooltip min-width="150" />
        <el-table-column prop="actualScore" label="实际得分" min-width="100" />
        <el-table-column prop="createTime" label="评估日期" min-width="150" />
      </el-table>
    </div>
    <add-daily-record v-if="showDailyRecord" ref="tipDlg" :visible.sync="showDailyRecord" :detail-data="activeRowData" @success="queryTableData(formData)" />
  </div>
</template>

<script>
import { queryDailyAssessment } from '@/api/constructionUnitMng'
// import { openPage } from '@/utils/index'
import addDailyRecord from './addDailyRecord.vue'
export default {
  name: 'DailyAssessment',
  components: { addDailyRecord },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    detailData: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      tableLoading: false,
      showDailyRecord: false,
      activeRowData: {},
      queryObj: { // 搜索表单
        productCode: '',
        productName: '',
        materialCode: '',
        materialName: '',
        colorSortingate: '',
        materialClass: '',
        changePosition: ''
      },
      otherFilters: [],
      tableData: [],
      formData: {},
      flag: '',
      selectData: {
        productCode: '',
        productName: ''
      }
    }
  },
  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  async created() {
    this.tableLoading = true
    this.queryObj = this.detailData.data
    this.flag = this.detailData.flag
    const otherFilter = [{ 'id': 'unitCode', 'property': 'unitCode', 'operator': '=', 'value': this.queryObj.unitCode }]
    const obj = { 'scoreNature': 'DAILY' }
    const formData = new FormData()
    formData.append('filtersRaw', JSON.stringify(otherFilter))
    formData.append('paramsRaw', JSON.stringify(obj))
    this.formData = formData
    this.queryTableData(formData)
  },
  activated() {
    this.selectData = {
      productCode: '',
      productName: ''
    }
  },
  methods: {
    /**
     * 表格合并
     * @param {*} param0
     * row 表格每一行的数据
     * column 表格每一列的数据
     * rowIndex 表格的行索引,不包括表头,从0开始
     * columnIndex 表格的列索引,从0开始
     */
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex <= 4 || columnIndex === 8 || columnIndex === 9) {
        // 过滤需要合并的行数
        if (row.spanIndex) {
          // 若rowIndex存在，则合并
          return {
            rowspan: row.spanIndex,
            colspan: 1
          }
        } else {
          return {
            rowspan: 0,
            colspan: 0
          }
        }
      }
    },
    queryTableData(data) {
      queryDailyAssessment(data).then(res => {
        if (res.success && res.data && res.data.length) {
          const arr = []
          res.data.forEach(row => {
            if (row.assessDetailsDtos && row.assessDetailsDtos.length) {
              row.assessDetailsDtos.forEach((item, index) => {
                const obj = {}
                obj.dataId = row.id
                obj.scoreCriteriaId = row.scoreCriteriaId
                obj.scoreType = row.scoreType
                obj.totalScore = row.totalScore
                obj.scoreDesc = row.scoreDesc
                obj.validStartTime = row.validStartTime.split(' ')[0] || ''
                obj.validEndTime = row.validEndTime.split(' ')[0] || ''
                obj.actualScore = row.actualScore
                obj.itemScoreDesc = item.scoreDesc
                obj.detailItem = item.detailItem
                obj.grading = item.grading
                obj.createTime = item.createTime.split(' ')[0]
                if (index === 0) {
                  obj.spanIndex = row.assessDetailsDtos.length
                }
                arr.push(obj)
              })
            }
          })
          this.tableData = arr
        }
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    handleEdit() {
      this.activeRowData.data = this.detailData.data
      this.showDailyRecord = true
    },
    handleCheckDetail() {
      const obj = {
        type: 'unitEdit',
        unitCode: this.detailData.data.unitCode
      }
      const path = '/constructionUnitMng/dailyAssessmentRecord'
      const query = obj
      // const pageName = '日常评估记录'
      // const menuCode = 'PC1298767'
      this.$store.dispatch('views/goPage', { path: path, query: query })
      // openPage(path, query, pageName, menuCode)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
